package com.minsheng.epidemic_rep.service.impl;

import com.minsheng.epidemic_rep.dao.FeverClinicsDao;
import com.minsheng.epidemic_rep.dao.FrmzzlcfDao;
import com.minsheng.epidemic_rep.entity.FeverClinics;
import com.minsheng.epidemic_rep.entity.Frmzzlcf;
import com.minsheng.epidemic_rep.service.IFrmzzlcfService;
import com.minsheng.epidemic_rep.util.GenerateCsv;
import com.minsheng.epidemic_rep.util.ZipUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Classname FrmzzlcfServiceImpl
 * @Description 发热门诊诊疗处方
 * @Date 2020/10/1 21:37
 * @Created by You
 */
@Service("frmzzlcfService")
public class FrmzzlcfServiceImpl implements IFrmzzlcfService {


    @Autowired
    private FrmzzlcfDao frmzzlcfDao;

    @Scheduled(cron = "0 52 15 * * ?")
    @Override
    public void createCsv() throws ParseException {

        System.out.println("hello!");

        //文件头名称（字段名）
        List<String> colName = new ArrayList<String>();
        colName.add("P7502");
        colName.add("P7506");
        colName.add("P7000");
        colName.add("P4");
        colName.add("P7800");
        colName.add("P7805");
        colName.add("P7806");
        colName.add("P7815");
        colName.add("P7817");
        colName.add("P7818");



        //日期格式转换
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //从数据库获取发热门诊病人数据
        List<Frmzzlcf> frmzzlcfList = new ArrayList<Frmzzlcf>();

        //获取今天下午5点和7天前时间参数
        String endTime = sdf1.format(new Date()) + " 00:00:00";
        Date endDateTime = sdf1.parse(endTime);//结束时间是今天下午5点

        //获取7天前时间参数
        Calendar cal = Calendar.getInstance();
        cal.setTime(endDateTime);
        cal.add(Calendar.DATE,-7);
        Date startDateTime = cal.getTime();//起始时间是七天前下午5点
        String startTime = sdf2.format(startDateTime);
        System.out.println(startTime);

        frmzzlcfList = frmzzlcfDao.findAll(startTime,endTime);

        //测试数据
//        FeverClinics feve = new FeverClinics("123", "123", "123", "123", "123", "123", "123", new Date(), 000, "123", "123", "123", "123", "123", "123", new Date(), "123", "123", "123", "123", "123", 12345.678);
//        feverClinicsList.add(feve);

        System.out.println(frmzzlcfList);

        //存放所有数据列表
        List<List<String>> allList = new ArrayList<List<String>>();


        for(Frmzzlcf frmzzlcf : frmzzlcfList){
            //将查询到实体数据的属性拆开转成字符串存放到列表中
            List<String> list = new ArrayList<String>();
            list.add(frmzzlcf.getP7502());
            list.add(frmzzlcf.getP7506());
            list.add(frmzzlcf.getP7000());
            list.add(frmzzlcf.getP4());
            list.add(frmzzlcf.getP7800());
            list.add(frmzzlcf.getP7805());
            list.add(frmzzlcf.getP7806());
            list.add(frmzzlcf.getP7815());
            list.add(frmzzlcf.getP7817());
            list.add(frmzzlcf.getP7818());

//            if(feverClinics.getP6() == null){
//                list.add("-");
//            } else {
//                list.add(sdf1.format(feverClinics.getP6()));
//            }
//            list.add(feverClinics.getP7());//年龄，Integer

            //将拆分后的数据放入到总数据列表中
            allList.add(list);
        }

        //生成日期后缀
        String dateStr = sdf1.format(new Date());

        try {
            GenerateCsv.generateCsv("d:/frmzsj/","drug_" + dateStr,colName,allList);
        } catch (IOException e) {
            e.printStackTrace();
        }

//-------------------------------------压缩ZIP文件
        //获取待压缩文件列表(文件具体路径)
//        List<String> pathList = ZipUtil.getFile("d:/frmzsj/",0);
//
//        try {
//            ZipUtil.zipFiles(pathList,"d:/frmzsj/发热门诊数据.zip");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }



}
